import os
import fnmatch
import re

def find_files(directory, pattern):
    """
    遍历指定目录及其子目录，查找匹配特定模式的文件。
    
    :param directory: 要搜索的目录路径。
    :param pattern: 要匹配的文件模式，例如'*.txt'。
    :return: 匹配的文件列表。
    """
    matches = []
    for root, dirnames, filenames in os.walk(directory):
        for filename in fnmatch.filter(filenames, pattern):
            matches.append(os.path.join(root, filename))
    return matches

# 使用示例
with open(r'eu5/game_path.txt', 'r', encoding='utf-8') as file:
    game_path = file.read()  # 读取全部内容到一个字符串
directory_path = game_path + r"/game/main_menu/gui"  # 替换为你的目录路径
file_pattern = '*.gui'  # 替换为你需要查找的文件后缀名
pattern = r'fontsize = (\d+)'
found_files = find_files(directory_path, file_pattern)

for file_path in found_files:
    # print(file_path)
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    def replace_fontsize(match):
        fontsize = int(match.group(1))  # 从匹配对象中获取数字，并转换为整数
        if fontsize < 20:
            return 'fontsize = 20'
        else:
            return match.group(0)  # 如果fontsize不小于20，保持原样

    new_content = re.sub(pattern, replace_fontsize, content)
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(new_content)

pattern = r'fontsize_min = (\d+)'
found_files = find_files(directory_path, file_pattern)

for file_path in found_files:
    # print(file_path)
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    def replace_fontsize(match):
        fontsize_min = int(match.group(1))  # 从匹配对象中获取数字，并转换为整数
        if fontsize_min < 20:
            return 'fontsize_min = 20'
        else:
            return match.group(0)  # 如果fontsize不小于20，保持原样

    new_content = re.sub(pattern, replace_fontsize, content)
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(new_content)